<?php
/* 
 * Generates the user pages of the admin dashboard.
 */

/**
 * @author ChristianNotebook
 */
class Users extends CI_Controller
{
    function Users()
    {
        parent::__construct();

        session_start();

       // checks if user is logged in.
       if ($this->session->userdata('userid') < 1)
        {
            redirect('/welcome/index', 'refresh');
        }
    }

    // generates the admin_user_home where it is possible to choose which user to edit or delete.
    function index(){
	$data['title'] = "Manage Users";
	$data['main_content'] = 'admin/admin_users_home';
	$data['user'] = $this->model_admins->getAllUsers();
	$this->load->vars($data);
	$this->load->view('admin/dashboard');
    }

    // generates the edit admin_users_edit view and updates users.
    // calles and sends and id to getUserByUsername in the users model. 
    function edit($id=0){
        if ($this->input->post('username')){
            $this->model_admins->update_user($id);
            $this->session->set_flashdata('message','User updated');
            redirect('admin/users/index','refresh');
  	}
        else {
            $data['title'] = "Edit User";
            $data['main_content'] = 'admin/admin_users_edit';
            $data['admin'] = $this->model_users->getUserByUsername($id);
            if (!count($data['admin'])) {
		redirect('admin/users/index','refresh');
            }
            $this->load->vars($data);
            $this->load->view('admin/dashboard');
	}
    }
}
?>
